package com.util.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xin.ding
 * @date 2020/7/8 19:27
 */
public class DateUtils {
    public static final ThreadLocal<DateFormat> DATE_FORMAT = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
        }
    };

    public static final ThreadLocal<DateFormat> DATE_SIMPLE_FORMAT = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyyMMdd");
        }
    };

    public static final ThreadLocal<DateFormat> TIME_SIMPLE_FORMAT = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("HH:mm:ss");
        }
    };

    public static String currentDateString() {
        return DATE_FORMAT.get().format(new Date());
    }

    public static String toSimpleDateString(Date date) {
        return DATE_SIMPLE_FORMAT.get().format(date);
    }

    public static String toSimpleTimeString(Date date) {
        return TIME_SIMPLE_FORMAT.get().format(date);
    }

    public static void main(String[] args) {
        System.out.println(currentDateString());
        System.out.println(toSimpleDateString(new Date()));
        System.out.println(toSimpleTimeString(new Date()));
    }
}
